﻿using System;
using System.Web;
using System.Drawing;
using System.IO;

namespace AllNations
{
    /// <summary>
    /// Summary description for PhotoHandler
    /// </summary>
    public class PhotoHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            // Grab the comic ID from the query string and validate it
            string id = context.Request["PhotoId"];
            if (String.IsNullOrEmpty(id))
                return;

            Guid PhotoId = Guid.Empty;
            try
            {
                PhotoId = new Guid(id);
            }
            catch (FormatException)
            {
                return;
            }

            // Check for the image in the application cache
            byte[] image = null;

            MakeItWeb.Framework.Photo photo = MakeItWeb.Framework.Photo.GetPhoto(PhotoId);
            try
            {
                string sFilename = "";
                if (photo.Loaded)
                {
                    sFilename = photo.FilepathTxt;
                }

                int iWidth = 0;
                int iHeight = 0;
                int iSize = string.IsNullOrEmpty(context.Request.QueryString["Size"]) ? 120 : int.Parse(context.Request.QueryString["Size"]);

                Image oImg = System.Drawing.Image.FromFile(sFilename);
                if (iSize > Math.Max(oImg.Width, oImg.Height))
                    iSize = Math.Max(oImg.Width, oImg.Height);

                if (oImg.Width > oImg.Height)
                {
                    iWidth = iSize;
                    iHeight = (iWidth * oImg.Height) / oImg.Width;
                }
                else
                {
                    iHeight = iSize;
                    iWidth = (iHeight * oImg.Width) / oImg.Height;
                }

                Bitmap oTarget = new Bitmap(iWidth, iHeight);
                Graphics oThumbnail = Graphics.FromImage(oTarget);
                oThumbnail.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
                oThumbnail.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                oThumbnail.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                oThumbnail.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                oThumbnail.DrawImage(oImg, new Rectangle(0, 0, iWidth, iHeight));
                using (MemoryStream ms = new MemoryStream())
                {
                    oTarget.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                    image = ms.ToArray();
                }
                // clean up
                oImg.Dispose();
                oTarget.Dispose();
                oThumbnail.Dispose();

                // clean up the image file handle
                oImg.Dispose();
            }
            catch (Exception)
            {
                throw;
            }

            // Return the image in the HTTP response
            if (image != null)
            {
                context.Response.ContentType = "image/jpg";
                context.Response.OutputStream.Write(image, 0, image.Length);
                context.Response.Expires = 1440 * 7;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}